home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / logctl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  3.9 KB  |  183 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: logctl.c,v 5.1 1992/11/01 00:01:21 panos Exp $" ;
  8.  
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <syslog.h>
  12. #include <fcntl.h>
  13.  
  14. #include "sio.h"
  15. #include "xlog.h"
  16.  
  17. #include "config.h"
  18. #include "defs.h"
  19. #include "log.h"
  20. #include "service.h"
  21. #include "state.h"
  22.  
  23. void msg() ;
  24.  
  25.  
  26. /*
  27.  * NOTE: This function uses the current configuration to determine the
  28.  *            common log file
  29.  */
  30. status_e start_log( sp )
  31.     struct service *sp ;
  32. {
  33.     struct service_data *sdp = SDATA( sp ) ;
  34.     struct service_config *scp = CONF( sp ) ;
  35.     struct log *lp = LOG( scp ) ;
  36.     xlog_h xh ;
  37.     char *func = "start_log" ;
  38.     xlog_h start_filelog() ;
  39.     void log_in_error() ;
  40.  
  41.     switch ( lp->log_type )
  42.     {
  43.         case L_NONE:
  44.             xh = NULL ;
  45.  
  46.         case L_SYSLOG:
  47.             xh = xlog_create( XLOG_SYSLOG, scp->id, XLOG_NOFLAGS, 
  48.                                     SYSLOG( lp )->facility, SYSLOG( lp )->level ) ;
  49.             if ( xh == NULL )
  50.             {
  51.                 msg( LOG_ERR, func,
  52.                     "failed to create a log for service %s", scp->id ) ;
  53.                 return( FAILED ) ;
  54.             }
  55.             xlog_control( xh, XLOG_CALLBACK, log_in_error, (void *)sp ) ;
  56.             break ;
  57.         
  58.         case L_FILE:
  59.             /*
  60.              * NOTE: if the same file is specified for more than one
  61.              *            service, it will be opened as many times
  62.              *            Furthermore, size control will not be accurate.
  63.              */
  64.             xh = start_filelog( scp->id, XLOG_NOFLAGS, FILELOG( lp ) ) ;
  65.             if ( xh == NULL )
  66.                 return( FAILED ) ;
  67.             xlog_control( xh, XLOG_CALLBACK, log_in_error, (void *)sp ) ;
  68.             break ;
  69.         
  70.         case L_COMMON_FILE:
  71.             if ( DEFAULT_LOG( ps ) == NULL )
  72.                 if ( DEFAULT_LOG_ERROR( ps ) )
  73.                     return( FAILED ) ;
  74.                 else
  75.                 {
  76.                     xh = start_filelog( "default", XLOG_NOFLAGS,
  77.                                         FILELOG( LOG( DEFAULTS( ps ) ) ) ) ;
  78.                     if ( xh == NULL )
  79.                     {
  80.                         DEFAULT_LOG_ERROR( ps ) = TRUE ;
  81.                         return( FAILED ) ;
  82.                     }
  83.                     DEFAULT_LOG( ps ) = xh ;
  84.                     xlog_control( xh, XLOG_CALLBACK, log_in_error, VOID_NULL ) ;
  85.                 }
  86.             else
  87.                 xh = DEFAULT_LOG( ps ) ;
  88.             break ;
  89.  
  90.         default:            /* SHOULDN'T HAPPEN */
  91.             msg( LOG_ERR, func, "bad log type (%d) for service %s",
  92.                 (int) lp->log_type, scp->id ) ;
  93.             return( FAILED ) ;
  94.     }
  95.     sdp->log_handle = xh ;
  96.     return( OK ) ;
  97. }
  98.  
  99.  
  100. PRIVATE xlog_h start_filelog( id, flags, flp )
  101.     char *id ;
  102.     int flags ;
  103.     struct filelog *flp ;
  104. {
  105.     xlog_h xh ;
  106.     int fd ;
  107.     int log_file_mode = ( debug.on ) ? 0644 : LOG_FILE_MODE ;
  108.     char *func = "start_filelog" ;
  109.  
  110.     xh = xlog_create( XLOG_FILELOG, id, flags,
  111.                             flp->filename, LOG_OPEN_FLAGS, log_file_mode ) ;
  112.     if ( xh == NULL )
  113.     {
  114.         msg( LOG_ERR, func, "creation of %s log failed", id ) ;
  115.         return( NULL ) ;
  116.     }
  117.  
  118.     xlog_control( xh, XLOG_GETFD, &fd ) ;
  119.     if ( fcntl( fd,  F_SETFD, 1 ) == -1 )
  120.     {
  121.         msg( LOG_ERR, func, "fcntl F_SETFD: %m" ) ;
  122.         xlog_destroy( xh ) ;
  123.         return( NULL ) ;
  124.     }
  125.  
  126.     ps.rws.descriptors_free-- ;
  127.  
  128.     if ( FILELOG_SIZE_CONTROL( flp ) )
  129.         xlog_control( xh, XLOG_LIMITS, flp->soft_limit, flp->hard_limit ) ;
  130.  
  131.     return( xh ) ;
  132. }
  133.  
  134.  
  135. /*
  136.  * The only purpose of this function is to enter a log message about
  137.  * the xlog error.
  138.  * 
  139.  * NOTE: We could destroy the xlog at this point but we choose not to.
  140.  */
  141. PRIVATE void log_in_error( xh, error_code, arg )
  142.     xlog_h xh ;
  143.     int error_code ;
  144.     void *arg ;
  145. {
  146.     struct service *sp = SP( arg ) ;
  147.     char *log_id = ( sp == NULL ) ? "common" : CONF( sp )->id ;
  148.     char *func = "log_in_error" ;
  149.  
  150. #ifdef lint
  151.     xh = xh ;
  152. #endif
  153.     if ( error_code == XLOG_ESIZE )
  154.         msg( LOG_ERR, func, "Size of %s log exceeded hard limit", log_id ) ;
  155.     else
  156.         msg( LOG_ERR, func, "Error in %s log: %d", log_id, error_code ) ;
  157. }
  158.  
  159.  
  160. void end_log( type, xh )
  161.     logtype_e type ;
  162.     xlog_h xh ;
  163. {
  164.     char *func = "end_log" ;
  165.  
  166.     if ( xh == NULL )        /* shouldn't be NULL but just in case */
  167.     {
  168.         msg( LOG_NOTICE, func, "end_log called with NULL handle" ) ;
  169.         return ;
  170.     }
  171.  
  172.     switch ( type )
  173.     {
  174.         case L_FILE:
  175.             ps.rws.descriptors_free-- ;
  176.             /* FALL THROUGH */
  177.         
  178.         case L_SYSLOG:
  179.             xlog_destroy( xh ) ;
  180.     }
  181. }
  182.  
  183.